#include "caltimer.h"
#include "ui_caltimer.h"

CalTimer::CalTimer(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CalTimer)
{
    ui->setupUi(this);
    setupUiAppearance();
    connectButtonsAndSignals();
    bindTextlineWithSoftkeyBoard();

    labelEditedName = new QString("");
    labelEdited = new QLabel();
    for (int i = 0; i < ui->stackedWidget->count(); i++) {
        QWidget* page = ui->stackedWidget->widget(i);
        QList<QLabel*> labelEdits = page->findChildren<QLabel*>();
        for (QLabel* labelEdit : labelEdits) {
            labelEdit->setText("...");
        }
    }
}

void CalTimer::connectButtonsAndSignals()
{
    connect(ui->calTimerReturnMenu, &QPushButton::clicked, this, &CalTimer::pushReturnMenu);
    connect(ui->pushButton_2, &QPushButton::clicked, [&](){
        ui->stackedWidget->setCurrentIndex(0);
        setButtonClickedStyle(ui->pushButton_2); });
    connect(ui->pushButton_3, &QPushButton::clicked, [&](){
        ui->stackedWidget->setCurrentIndex(1);
        setButtonClickedStyle(ui->pushButton_3); });
    connect(ui->pushButton_4, &QPushButton::clicked, [&](){
        ui->stackedWidget->setCurrentIndex(2);
        setButtonClickedStyle(ui->pushButton_4); });
    connect(ui->pushButton_5, &QPushButton::clicked, [&](){
        ui->stackedWidget->setCurrentIndex(3);
        setButtonClickedStyle(ui->pushButton_5); });
    connect(ui->pushButton_6, &QPushButton::clicked, [&](){
        ui->stackedWidget->setCurrentIndex(4);
        setButtonClickedStyle(ui->pushButton_6); });

    connect(ui->toolButton_47, &softkeys::clear_windows, this, [this]() {
        ui->lineEdit_3->setText("");
    });
    connect(ui->toolButton_48, &softkeys::close_windows, this, [this]() {
        labelEdited->setStyleSheet("qproperty-alignment: 'AlignRight|AlignBottom'; margin-right: 3px; margin-bottom: 0px;");
        hideWidget2();
        ui->lineEdit_3->setText("");
    });
    connect(ui->toolButton_41, &softkeys::close_windows, this, [this]() {
        labelEdited->setStyleSheet("qproperty-alignment: 'AlignRight|AlignBottom'; margin-right: 3px; margin-bottom: 0px;");
        hideWidget2();
        updateLabel();
        ui->lineEdit_3->setText("");
    });
}

void CalTimer::re_setupUiAppearance()
{
    switch(ui->stackedWidget->currentIndex())
    {
        case 0: setButtonClickedStyle(ui->pushButton_2); break;
        case 1: setButtonClickedStyle(ui->pushButton_3); break;
        case 2: setButtonClickedStyle(ui->pushButton_4); break;
        case 3: setButtonClickedStyle(ui->pushButton_5); break;
        case 4: setButtonClickedStyle(ui->pushButton_6); break;
        default: setButtonClickedStyle(ui->pushButton_2); break;
    }
}

void CalTimer::setButtonClickedStyle(QPushButton* button)
{
    if(button != ui->pushButton_2)
        ui->pushButton_2->setStyleSheet("");
    else
        ui->pushButton_2->setStyleSheet("background-color: yellow");
    if(button != ui->pushButton_3)
        ui->pushButton_3->setStyleSheet("");
    else
        ui->pushButton_3->setStyleSheet("background-color: yellow");
    if(button != ui->pushButton_4)
        ui->pushButton_4->setStyleSheet("");
    else
        ui->pushButton_4->setStyleSheet("background-color: yellow");
    if(button != ui->pushButton_5)
        ui->pushButton_5->setStyleSheet("");
    else
        ui->pushButton_5->setStyleSheet("background-color: yellow");
    if(button != ui->pushButton_6)
        ui->pushButton_6->setStyleSheet("");
    else
        ui->pushButton_6->setStyleSheet("background-color: yellow");
}

void CalTimer::bindTextlineWithSoftkeyBoard()
{
    QList<QLabel*> labelList = ui->stackedWidget->currentWidget()->findChildren<QLabel*>();
    for (QLabel* label : labelList) {
        label->setFocus();
        label->installEventFilter(this);
    }
}

void CalTimer::setupUiAppearance()
{
    ui->widget->setAutoFillBackground(true);
    QPalette palette = ui->widget->palette();
    palette.setColor(QPalette::Background, Qt::white);
    ui->widget->setPalette(palette);
    ui->widget_2->hide();
}

void CalTimer::hideWidget2()
{
    ui->widget_2->hide();
}

void CalTimer::showWidget2()
{
    ui->widget_2->show();
    ui->lineEdit_3->setFocus();
}

void CalTimer::updateLabel()
{
    QLabel *labelEdit = findChild<QLabel*>(*labelEditedName);
    if(labelEdit && !ui->lineEdit_3->text().isEmpty())
    {
        labelEdit->setText(ui->lineEdit_3->text() + "s");
    }
}

bool CalTimer::eventFilter(QObject *watched, QEvent *event)
{
    if(event->type()==QEvent::MouseButtonRelease)
    {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
        if(mouseEvent->button() == Qt::LeftButton)
        {
            QList<QLabel*> labelList = ui->stackedWidget->currentWidget()->findChildren<QLabel*>();
            for (QLabel* label : labelList) {
                if(watched == label)
                {
                    labelEdited = qobject_cast<QLabel*>(watched);
                    if(!ui->widget_2->isVisible())
                    {
                        if(labelEdited->text() != "...")
                        {
                            QString labelEditedContent = labelEdited->text().toStdString().c_str();
                            labelEditedContent.chop(1);
                            ui->lineEdit_3->setText(labelEditedContent);
                        }
                        labelEdited->setStyleSheet("border: 3px solid #000011;");
                        *labelEditedName = watched->objectName();
                        ui->widget_2->show();
                    }
                }
            }
        }
    }
    return QWidget::eventFilter(watched,event);
}

CalTimer::~CalTimer()
{
    delete ui;
}

void CalTimer::pushReturnMenu()
{
    emit returnMenu();
}
